package com.company.finance.ddd.domain.core.bill.aggregate.assembler;

import com.company.finance.ddd.domain.core.bill.aggregate.BillAggregate;
import com.company.finance.ddd.domain.core.bill.aggregate.PayItem;
import com.company.finance.ddd.domain.core.voucher.aggregate.Voucher;
import com.company.finance.ddd.domain.core.voucher.rule.BillItemRule;
import com.company.finance.ddd.domain.core.voucher.rule.VoucherDigestSetter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wanglei
 */
public class PayItemAssembler implements ItemAssembler {

    @Override
    public List<Voucher.VoucherItem> assemble(BillItemRule billItemRule, BillAggregate billAggregate) {
        List<Voucher.VoucherItem> list = new ArrayList<>();

        for (Object item : billAggregate.getItems()) {
            PayItem payItem = (PayItem) item;

            VoucherDigestSetter placeholderSetter = ((VoucherDigestSetter) billItemRule.getCreditDigestRule().getLayoutSetter());
            placeholderSetter.setCompanyName(billAggregate.getBill().getCOMPANY_NAME());
            placeholderSetter.setEmpName(billAggregate.getBill().getEMPL_ID().toString());

            list.add(Voucher.VoucherItem.builder()
                    .ORIGINAL_AMOUNT(payItem.getPAYMENT_AMOUNT())
                    .CREDIT_AMOUNT(payItem.getPAYMENT_AMOUNT())
                    .DIGEST(billItemRule.getCreditDigestRule().setDigestSetter(placeholderSetter).buildDigest())
                    .ACC_SUBJECT(billItemRule.getCreditSubject().getCode())
                    .ACC_SUBJECT_NAME(billItemRule.getCreditSubject().getName())
                    .build());

            list.add(Voucher.VoucherItem.builder()
                    .ORIGINAL_AMOUNT(payItem.getPAYMENT_AMOUNT().negate())
                    .DEBIT_AMOUNT(payItem.getPAYMENT_AMOUNT().negate())
                    .DIGEST(billItemRule.getDebitDigestRule().setDigestSetter(placeholderSetter).buildDigest())
                    .ACC_SUBJECT(billItemRule.getDebitSubject().getCode())
                    .ACC_SUBJECT_NAME(billItemRule.getDebitSubject().getName())
                    .build());
        }

        return list;
    }
}
